package com.exsun.ex1077.ui.activity.bounds

import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONObject
import com.drake.brv.PageRefreshLayout
import com.exsun.ex1077.app.Constant
import com.exsun.ex1077.network.apiService
import com.exsun.ex1077.network.entity.Bounds
import com.exsun.ex1077.network.entity.Page
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.extend.base.ResultState
import com.zhangping.fastjetpack.extend.base.request
import com.zhangping.fastjetpack.extend.databind.BooleanObservableField

/**
 * 描述：
 * 作者：ZhangPing
 * 创建时间：2023/3/27 10:25
 */
class BoundsListViewModel: BaseViewModel() {

    val pageSize = 20
    val dataLiveData = MutableLiveData<ResultState<Page<Bounds>>>()

    fun loadData(index: Int = PageRefreshLayout.startIndex, searchWord: String = ""){
        request({ apiService.boundsList(JSONObject().apply {
            put("pageIndex", index)
            put("pageSize", pageSize)
            put("type", Constant.BOUNDS_TYPE_POLYGON)
            if (searchWord.isNotEmpty()){
                put("name", searchWord)
            }
        })}, dataLiveData)
    }

    fun boundsDelete(id: String): MutableLiveData<Boolean> {
        val liveData = MutableLiveData<Boolean>()
        request({ apiService.boundsDelete(id)}, {
            liveData.value = true
        }, isShowDialog = true)
        return liveData
    }

    fun boundsRename(id: String, name: String): MutableLiveData<Boolean>{
        val liveData = MutableLiveData<Boolean>()
        request({ apiService.boundsRename(JSONObject().apply {
            put("id", id)
            put("name", name)
        })}, {
            liveData.value = true
        })
        return liveData
    }

    fun boundsQuery(id: String): MutableLiveData<Bounds>{
        val liveData = MutableLiveData<Bounds>()
        request({ apiService.boundsQuery(id)}, {
            it?.let { liveData.value = it }
        })
        return liveData
    }

}